//
//  RecommendMenuBarView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/18.
//

import UIKit

class RecommendMenuBarView: UIView {
    private let state = UIView()
    private var buttons:Array<UIButton> = []
    private var index = 0
    var tabSelectedChanged:((Int)->Void)?
    private let menusBar = UIScrollView()
    init(titles:Array<String>) {
        super.init(frame: .zero)
        self.backgroundColor = .white
        state.backgroundColor = RED_COLOR
        
        menusBar.delegate = self
        menusBar.showsHorizontalScrollIndicator = false
        addSubview(menusBar)
        menusBar.snp.makeConstraints({
            $0.height.width.edges.equalToSuperview()
        })
        var temp:UIButton?
        for i in 0..<titles.count {
            let btn = UIButton(type: .custom)
            buttons.append(btn)
            btn.setTitleColor(B000_COLOR, for: .normal)
            btn.setTitle(titles[i], for: .normal)
            btn.font = Font_Size_17
            menusBar.addSubview(btn)
            btn.snp.makeConstraints({
                if temp == nil{
                    $0.leading.equalTo(20)
                }else{
                    $0.leading.equalTo(temp!.snp.trailing).offset(30)
                    $0.width.equalTo(temp!)
                }
                if i == titles.count-1{
                    $0.trailing.equalToSuperview().inset(20)
                }
                $0.centerY.equalToSuperview()
            })
            btn.addTarget(self, action: #selector(buttonTap(btn:)), for: .touchUpInside)
            temp = btn
        }
        menusBar.addSubview(state)
        state.snp.makeConstraints({
            $0.centerX.equalTo(buttons.first!)
            $0.top.equalTo(47)
            $0.width.equalTo(buttons.first!).dividedBy(2)
            $0.height.equalTo(2)
        })
    }

    @objc func buttonTap(btn:UIButton){
        self.updateFrame(btn)
        self.tabSelectedChanged?(index)
    }
    
    private func updateFrame(_ btn:UIButton) {
        for item in buttons {
            item.font = Font_Size_17
        }
        index = buttons.firstIndex(of: btn)!
        btn.font = getLabelFont(17,blod: true)
        state.snp.remakeConstraints({
            $0.centerX.equalTo(btn)
            $0.top.equalTo(47)
            $0.width.equalTo(buttons.first!).dividedBy(2)
            $0.height.equalTo(2)
        })
        UIView.animate(withDuration: 0.5) {
            [weak self] in
            self?.layoutIfNeeded()
        }
        let bx = btn.frame.origin.x
        let bw = btn.frame.size.width
        let w = menusBar.frame.width

        if bx + bw/2 > w/2 && bx + bw/2 - w/2 < menusBar.contentSize.width - w {
            menusBar.setContentOffset(CGPoint.init(x: bx + bw/2 - w/2, y: 0), animated: true)
        }else{
            if(bx + bw/2 - w/2 < menusBar.contentSize.width - w){
                menusBar.setContentOffset(CGPoint.zero, animated: true)
            }else{
                menusBar.setContentOffset(CGPoint.init(x: menusBar.contentSize.width-w, y: 0), animated: true)
            }
        }
    }
    func nextTab(_ index:Int)  {
        self.updateFrame(buttons[index])
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let cl = CALayer()
        cl.backgroundColor = Separator_COLOR.cgColor
        cl.frame = CGRect.init(x: 0, y: bounds.height-1, width: bounds.width, height: 1)
        self.layer.addSublayer(cl)
    }
    required init?(coder: NSCoder) {
        fatalError()
    }
}
extension RecommendMenuBarView:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    }
}
